<template>
    <transition name="fade-dialog" appear @after-enter="afterEnter">
        <div class="rx-alert" :class="{'rx-alert-open': alertShow}">
            <div class="rx-alert-close" @click="click({confirm:false})" v-if="showClose"></div>
            <div class="rx-alert-icon"></div>
            <div class="rx-alert-title" v-html="title"></div>
            <div class="rx-alert-content" v-html="text" v-if="text != ''"></div>
            <div class="rx-alert-btns" :class="btnLeft && 'btn-left'">            
                <!-- :style="item.hasColor" -->
                <div class="btn" 
                    v-for="(item, index) in btns"
                    :class="[item.confirm && 'primary', item.text.length <= 2 && 'text-justfity']"
                    :key="index"    
                    @click="click(item)">
                    {{item.text}}
                </div>
            </div>
        </div>
    </transition>
</template>

<script type="text/ecmascript-6">
function getType (str) {
    return Object.prototype.toString.call(str).split(' ')[1].split(']')[0];
}
export default {
    props: {
        title: '',
        icon: {
            default: 'warning'
        },
        btnLeft: {
            default: false
        },
        showClose: {
            default: false
        },
        text: '',
        btns: [String, Object, Array]
    },
    data() {
        return {
            alertShow: false
        }
    },
    mounted() {
        this.alertShow = true;
    },
    methods: {
        click (btn) {
            this.alertShow = false;
            setTimeout(() => {
                this.$emit('resolve', {confirm: btn.confirm, btn: btn.text});
                this.$nextTick(res=>{
                    this.$rxAlert.show = false;
                })
            }, 100);
        },
        afterEnter () {
            
        }
    },
    created () {
        let btnType = getType(this.btns),
            textType = getType(this.text),
            cancel = {
                text: '取消',
                // color: '',
                confirm: false,
            },
            confirm = {
                text: '确定',
                // color: 'primary',
                confirm: true,
            };
        // 处理没传按钮时
        if (!this.btns) { 
            let btn1 = Object.assign({},cancel),
                btn2 = Object.assign({},confirm);
            this.btns = [btn1, btn2];
        } else if (btnType == 'String') {
            let btn = Object.assign({}, confirm);
            btn.text = this.btns;
            this.btns = [btn];
        } else if(btnType == 'Object') {
            for (let key in confirm) {
                if (!this.btns.hasOwnProperty(key)) {
                    this.btns[key] = confirm[key];
                }
            }
            this.btns = [this.btns];
        }
        // 处理按钮颜色
        this.btns = this.btns.map( (item, i, arr) => {
            if (typeof item == 'string') {
                item = {
                    text: item,
                    // color: i == arr.length - 1 ? 'primary' : '',
                    confirm: i == arr.length -1
                }
            }
            for (let key in cancel) {
                if (!item.hasOwnProperty(key)) {
                    item[key] = cancel[key];
                }
            }
            // if (item.color.indexOf('#') != -1) {
            //     item.hasColor = {'background-color': item.color, 'border-color': item.color, 'color': '#fff'};
            // }
            return item;
        });
    },
}
</script>

<style scoped lang="scss">
    .rx-alert{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -60%);
        padding: 40px;
        padding-left: 82px;
        border-radius: 10px;
        box-shadow: 0 2px 8px 0 rgba(0, 0, 0, .1);
        min-width: 420px;
        max-width: 80%;
        overflow: auto;
        background: #fff;
        opacity: .3;
        transition: all .2s linear;
        &.rx-alert-open{
            transform: translate(-50%, -50%);
            opacity: 1;
        }
        .rx-alert-close{
            position: absolute;
            top: 21px;
            right: 21px;
            width:14px;
            height: 14px;
            cursor: pointer;
            &::before,
            &::after{
                content: '';
                position: absolute;
                top: 50%;
                left: 0;
                transform-origin: center center;
                transform: translateY(-50%) rotateZ(45deg);
                border-radius: 17px;
                width: 17px;
                height: 2px;
                background-color: #ccc;
            }
            &::after{
                transform: translateY(-50%) rotateZ(-45deg);
            }
        }
        .rx-alert-icon{
            position: absolute;
            top: 40px;
            left: 40px;
            width: 32px;
            height: 32px;
            background: #fff url() no-repeat center / 116%;
            font-size: 32px;
        }
        .rx-alert-title{
            padding-bottom: 10px;
            font-size: 16px;
            font-family:PingFangSC-Medium;
            color: #333;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
        .rx-alert-content{
            overflow: hidden;
            word-break:break-all;
        }
        .rx-alert-btns{
            padding-top: 35px;
            text-align: right;
            &.btn-left{
                text-align: left;
            }
            .btn{
                display: inline-block;
                padding: 9px 20px 8px 20px;
                border: 1px solid #c4cad7;
                border-radius: 4px;
                min-width: 80px;
                font-size: 13px;
                line-height: 13px;
                color: #666;
                cursor: pointer;
                text-align: center;
                &:not(:last-child){
                    margin-right: 10px;
                }
                &.primary{
                    border-color: #5991ff;
                    background-color: #5991ff;
                    color: #fff;
                }
                &.text-justfity{
                    padding-left: 24px;
                    padding-right: 23px;
                    text-align: justify;
                    text-align-last: justify;
                }
            }
        }
    }
    .fade-dialog-enter-active, .fade-dialog-leave-active {
        .rx-alert{
            transform: translate(-50%, -60%);
            transition: all .3s linear;
        }
    }
    .fade-dialog-enter, .fade-dialog-leave-active {
        .rx-alert{
            transform: scale(-50%, -50%);
            transition: all .3s linear;
        }
    }
</style>
